<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>品牌列表案例</title>
    <script src="vue2610.js"></script>
    <style>
        #addView{
            width: 800px;
            margin: 20px auto;
        }
        #tb{
            width: 800px;
            border-collapse: collapse;
            margin: 20px auto;
        }
        #tb th{
            background-color: #0094ff;
            color: white;
            font-size: 16px;
            padding: 5px;
            text-align: center;
            border: 1px solid black;
        }
        #tb td{
            font-size: 16px;
            padding: 5px;
            text-align: center;
            border: 1px solid black;
        }
    </style>
</head>

<body>
<div id="app">
    <div id="addView">
        <input type="text" v-model="pid">
        <input type="text" v-model="pname">
        <button @click="addData">添加数据</button>
        <div>
            <input type="text" v-model="sname" placeholder="过滤条件">
        </div>
    </div>
    <table id="tb">
        <tr>
        <th>编号</th>
        <th>名称</th>
        <th>创建时间</th>
        <th>操作</th>
        </tr>
        <td colspan="4" v-if="list.length == 0">
            当前列表暂无数据
        </td>
        <tr v-for="item in filterItem(list)">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td>
<!--                <a href="#" v-on:click="test(index)">删除</a>-->
                <a href="#" v-on:click="test(item.id)">删除</a>
            </td>
        </tr>
    </table>
</div>
</body>


<script>
    let vm = new Vue({
        el:'#app',
        data:{
            list:[{id:1,name:'奔驰',ctime:new Date},
                {id:2,name:'酷奇',ctime:new Date},
                {id:3,name:'奇瑞',ctime:new Date}
            ],
            pid:0,
            pname:'',
            sname:'',
        },
        methods:{
            test (index) {
                //提醒弹窗
                if (!confirm('是否要删除数据')){
                    return
                }
               let di = this.list.findIndex(function (item) {
                    return item.id === index
                })
                this.list.splice(di,1)

                // this.list.splice(index,1)
            },
            addData () {
                this.list.push({id:this.pid,name:this.pname,ctime:new Date})
                this.pid = 0
                this.pname = ''
            },
            filterItem (item) {
                let t = this.sname
                let r = item.filter(function (obj) {
                    return  obj.name.indexOf(t) !== -1
                })
                return r

            }
        },
    });
</script>
</html>